NOTE: Your code should "recreate" representative results shown for graph cuts in Topic 9, but they do not have to be identical.
g = maxflow.GraphFloat()
nodeids = g.add_grid_nodes((num_rows, num_cols))
structure_x = np.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]])
structure_y = np.array([[0, 0, 0], [0, 0, 0], [0, 1, 0]])
g.add_grid_edges(nodeids, weights=n_right, structure=structure_x, symmetric=True)
g.add_grid_edges(nodeids, weights=n_below, structure=structure_y, symmetric=True)
g.add_grid_tedges( nodeids, t_source, t_sink )
tlinks_source = np.zeros((num_rows,num_cols))
tlinks_sink = np.zeros((num_rows,num_cols))
...
...
g.add_grid_tedges( nodeids, new_weights_source - tlinks_source, new_weights_sink - tlinks_sink)
tlinks_source = new_weights_source
tlinks_sink = new_weights_sink
%matplotlib widget
# loading standard modules
import numpy as np
import matplotlib.pyplot as plt
import maxflow
from skimage import img_as_ubyte
from sklearn import mixture
from skimage.color import rgb2gray
# loading custom module (requires file asg1.py in the same directory as the notebook file)
from asg1_error_handling import Figure, GraphCutsPresenter
class MyGraphCuts:
bgr_value = 0
obj_value = 1
none_value = 2
def __init__(self, img, sigma:float = 0.5, lam :float =1, gamma: float =0.5):
self.fig = Figure()
self.pres = GraphCutsPresenter(img, self)
self.pres.connect_figure(self.fig)
self.img = img
self.num_rows = img.shape[0]
self.num_cols = img.shape[1]
self.sigma = sigma
self.lam = lam
self.gamma = gamma
def run(self):
self.fig.show()
def compute_labels(self, seed_mask):
num_rows = self.num_rows
num_cols = self.num_cols
# +---------+---------+
# | | |
# | bgr | none |
# | | |
# +---------+---------+
# | | |
# | none | obj |
# | | |
# +---------+---------+
label_mask = np.full((num_rows, num_cols), self.none_value, dtype='uint8')
label_mask[:num_rows // 2, :num_cols // 2] = self.bgr_value
label_mask[num_rows // 2:, num_cols // 2:] = self.obj_value
return self.milestone_2(seed_mask) #label_mask
def milestone_1(self, seed_mask):
sigma = self.sigma
lam = self.lam
intensities = np.linalg.norm(x=self.img, axis=2)
right = np.square(intensities - np.roll(a=intensities, shift=-1, axis=0)) / (-2 * sigma ** 2)
below = np.square(intensities - np.roll(a=intensities, shift=-1, axis=1)) / (-2 * sigma ** 2)
# mask_right = np.where(seed_mask == np.roll(a=seed_mask, shift=-1, axis=0), 0, 1)
# mask_below = np.where(seed_mask == np.roll(a=seed_mask, shift=-1, axis=1), 0, 1)
# n_right = np.multiply(np.exp(right), mask_right)
# n_below = np.multiply(np.exp(below), mask_below)
n_right = np.exp(right) * lam
n_below = np.exp(below) * lam
t_source = np.where(seed_mask == self.bgr_value, np.infty, 0)
t_sink = np.where(seed_mask == self.obj_value, np.infty, 0)
g = maxflow.GraphFloat()
nodeids = g.add_grid_nodes((self.num_rows, self.num_cols))
structure_x = np.reshape(a=[0,0,0,0,0,1,0,0,0], newshape=(3,3))
structure_y = np.reshape(a=[0,0,0,0,0,0,0,1,0], newshape=(3,3))
g.add_grid_edges(nodeids, weights=n_right, structure=structure_x, symmetric=True)
g.add_grid_edges(nodeids, weights=n_below, structure=structure_y, symmetric=True)
g.add_grid_tedges(nodeids, t_source, t_sink)
g.maxflow()
seg = g.get_grid_segments(nodeids)
return np.array(seg, dtype=int)
def milestone_2(self, seed_mask):
sigma = self.sigma
lam = self.lam
gamma = self.gamma
intensities = np.linalg.norm(x=self.img, axis=2)
right = np.square(intensities - np.roll(a=intensities, shift=-1, axis=0)) / (-2 * sigma ** 2)
below = np.square(intensities - np.roll(a=intensities, shift=-1, axis=1)) / (-2 * sigma ** 2)
n_right = np.exp(right) * lam
n_below = np.exp(below) * lam
t_source = np.where(seed_mask == self.bgr_value, np.infty, 0) # bgr=0
t_sink = np.where(seed_mask == self.obj_value, np.infty, 0)
g = maxflow.GraphFloat()
nodeids = g.add_grid_nodes((self.num_rows, self.num_cols))
structure_x = np.reshape(a=[0,0,0,0,0,1,0,0,0], newshape=(3,3))
structure_y = np.reshape(a=[0,0,0,0,0,0,0,1,0], newshape=(3,3))
g.add_grid_edges(nodeids, weights=n_right, structure=structure_x, symmetric=True)
g.add_grid_edges(nodeids, weights=n_below, structure=structure_y, symmetric=True)
## T-Links
g.add_grid_tedges(nodeids, t_source, t_sink)
# Method 2
bgr_gmm = mixture.GaussianMixture(n_components=6)
obj_gmm = mixture.GaussianMixture(n_components=6)
bgr_mask = np.array(seed_mask == self.obj_value)
obj_mask = np.array(seed_mask == self.bgr_value)
if bgr_mask.sum() > 0 and obj_mask.sum() > 0:
bgr_samples = np.reshape(self.img[bgr_mask], newshape=(bgr_mask.sum(), self.img.shape[-1]))
obj_samples = np.reshape(self.img[obj_mask], newshape=(obj_mask.sum(), self.img.shape[-1]))
bgr_gmm.fit(bgr_samples)
obj_gmm.fit(obj_samples)
im_2d = np.reshape(self.img, newshape=(self.num_cols*self.num_rows, self.img.shape[-1]))
bgr_lhood = np.exp(bgr_gmm.score_samples(im_2d))
obj_lhood = np.exp(obj_gmm.score_samples(im_2d))
bgr_llhood = -np.log(gamma*bgr_lhood + (1-gamma)/256)
obj_llhood = -np.log(gamma*obj_lhood + (1-gamma)/256)
bgr_llhood_im = np.reshape(bgr_llhood, newshape=(self.num_rows, self.num_cols))
obj_llhood_im = np.reshape(obj_llhood, newshape=(self.num_rows, self.num_cols))
g.add_grid_tedges(nodeids, bgr_llhood_im, obj_llhood_im)
g.maxflow()
seg = g.get_grid_segments(nodeids)
return np.array(seg, dtype=int)
img1 = plt.imread('images/bunny.bmp')
app = MyGraphCuts(img1)
app.run()
c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn(
img1_1 = plt.imread('images/bunny.bmp')
app = MyGraphCuts(img1_1, sigma=0.05)
app.run()
c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn(
img1_2 = plt.imread('images/bunny.bmp')
app = MyGraphCuts(img1_2, sigma=0.8)
app.run()
c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn(
Comparing the cases $\sigma=0.5, 0.05, 1$, I found that the second case required a lot more seeds compared to the other two for segmentation. It also seems to follow the boundary between the object and background much better. One thing interesting about the third case is that the eyes are also part of the bunny unlike the other two cases.
img2 = plt.imread('images/lama.jpg')
app = MyGraphCuts(img2, lam=0)
app.run()
c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn(
img2_1 = plt.imread('images/lama.jpg')
app = MyGraphCuts(img2_1, lam=0.5)
app.run()
c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn(
img2_2 = plt.imread('images/lama.jpg')
app = MyGraphCuts(img2_2, lam=1)
app.run()
c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=2. warnings.warn( c:\Users\aaparda\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=3. warnings.warn(
Adding regulization improved the results while requiring less seeds. However, a lot more of the background was being mistaken as the object as a result.